package com.itheima.Tesk;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Component
public class Mytesk {
    @Autowired
    RedisTemplate redisTemplate;
    @Scheduled(cron = "0 0/1 * * * ? ")
    //判断垃圾图片
    public void phototsek(){
        //集合相减找出垃圾图片
        log.info("开始执行定时任务");
        try {
            Set <String>photo = redisTemplate.opsForSet().members("photo");//上传阿里云服务器的图片
            Set <String>phtot1 = redisTemplate.opsForSet().members("photo1");//新增数据库里面存在的图片
            Set <String>phtot2 = redisTemplate.opsForSet().members("photo2");//修改数据库里面存在的图片
            if (photo==null||phtot1==null){
                log.info("存在空集合不行删除");
                return;
            }
            //集合过滤出阿里云上多余的图片
            Set<String> garbagePhotos = photo.stream()
                    .filter(item -> !phtot1.contains(item))
                    .collect(Collectors.toSet());
            log.info("垃圾图片的路径集合:{}",garbagePhotos);
            log.info("垃圾图片的路径集合:{}",garbagePhotos.size());
            //集合过滤出阿里云上多余的图片
            Set<String> garbagePhotos1 = photo.stream()
                    .filter(item -> !phtot2.contains(item))
                    .collect(Collectors.toSet());
            log.info("垃圾图片的路径集合:{}",garbagePhotos1);
            log.info("垃圾图片的路径集合:{}",garbagePhotos1.size());
        } catch (Exception e) {
            log.info("定时任务执行失败");
        }

    }
}
